<template>
  <div class="tenantManagement-container">

    <el-collapse-transition>
    <div class="more-query" v-show="this.moreQueryFlag">
      <!-- 更多查找 -->
      <vab-query-form>
        <vab-query-form-left-panel :span="24">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>

            <el-form-item>
              <el-date-picker
                      v-model="endTimeDatePicker"
                      type="datetimerange"
                      :picker-options="pickerOptions"
                      range-separator="至"
                      start-placeholder="开始竞赛结束时间"
                      end-placeholder="结束竞赛结束时间"
                      align="right">
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-date-picker
                      v-model="signupEndTimeDatePicker"
                      type="datetimerange"
                      :picker-options="pickerOptions"
                      range-separator="至"
                      start-placeholder="开始报名截止时间"
                      end-placeholder="结束报名截止时间"
                      align="right">
              </el-date-picker>
            </el-form-item>


          </el-form>
        </vab-query-form-left-panel>

      </vab-query-form>
      <el-divider></el-divider>
    </div>
    </el-collapse-transition>

    <!-- 主要操作  -->
    <vab-query-form>
      <vab-query-form-left-panel :span="10">
        <el-button
            v-if="$perms('website_competition_insert')"
            icon="el-icon-plus"
            type="primary"
            @click="handleInsert"
        > 添加 </el-button>

        <el-button
            v-if="$perms('website_competition_import')"
            icon="el-icon-upload2"
            type="warning"
            @click="handleImportExcel"
        > 导入 </el-button>

        <el-button
            v-if="$perms('website_competition_export')"
            icon="el-icon-download"
            type="warning"
            @click="handleExportExcel"
        > 导出 </el-button>

        <el-button
            v-if="$perms('website_competition_delete')"
            :disabled="!selectRows.length > 0"
            icon="el-icon-delete"
            type="danger"
            @click="handleDelete"
        > 批量删除 </el-button>

      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="14">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>

          <el-form-item>
            <el-input
                    v-model.trim="queryForm.name_LIKE"
                    placeholder="请输入竞赛名称"
                    clearable
            />
          </el-form-item>

          <el-form-item>
            <el-date-picker
                    v-model="startTimeDatePicker"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始竞赛开始时间"
                    end-placeholder="结束竞赛开始时间"
                    align="right">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>

            <el-button icon="el-icon-search" @click="moreQuery">
              更多
            </el-button>

          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>

      <el-table-column show-overflow-tooltip label="序号" width="95">
        <template slot-scope="scope">
          {{(queryForm.pageNo - 1) * queryForm.pageSize + scope.$index + 1}}
        </template>
      </el-table-column>

      <el-table-column
              show-overflow-tooltip
              prop="name"
              label="竞赛名称"
      ></el-table-column>

      <el-table-column
              show-overflow-tooltip
              prop="startTime"
              label="竞赛开始时间"
      ></el-table-column>

      <el-table-column
              show-overflow-tooltip
              prop="endTime"
              label="竞赛结束时间"
      ></el-table-column>

      <el-table-column
              show-overflow-tooltip
              prop="signupEndTime"
              label="报名截止时间"
      ></el-table-column>

      <el-table-column
              show-overflow-tooltip
              prop="link"
              label="竞赛链接"
      ></el-table-column>

      <el-table-column
              show-overflow-tooltip
              prop="location"
              label="地点"
      ></el-table-column>

      <el-table-column
              show-overflow-tooltip
              prop="status"
              label="审核状态"
      ></el-table-column>


      <el-table-column
        show-overflow-tooltip
        label="操作"
        width="200"
        v-if="$perms('website_competition_update') || $perms('website_competition_delete')"
      >
        <template v-slot="scope">
          <el-button
            v-if="$perms('website_competition_update')"
            type="text"
            @click="handleUpdate(scope.row)"
          > 编辑 </el-button>
          
          <el-divider direction="vertical"></el-divider>
          
          <el-button
            v-if="$perms('website_competition_delete')"
            type="text"
            @click="handleDelete(scope.row)"
          > 删除 </el-button>
        </template>

      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>

    <edit ref="edit" @fetchData="fetchData"></edit>
    <import ref="import" @fetchData="fetchData" ></import>

  </div>
</template>

<script>
  import { getList, doDelete, doDeleteAll, doExportExcel } from "@/api/website/competition/CompetitionManagementApi";
  import Edit from "./components/CompetitionManagementEdit";
  import Import from "./components/CompetitionManagementImport";

  import { vueButtonClickBan } from "@/utils";
  import { isNotNull } from "@/utils/valiargs";
  import { formateDate } from "@/utils/format";

  export default {
    name: "CompetitionManagement",
    components: { Edit, Import },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: "total, prev, pager, next, sizes, jumper",
        total: 0,
        selectRows: "",
        elementLoadingText: "正在加载...",
        moreQueryFlag: false,
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          name_LIKE: "",
          startTime_BEGIN: "",
          startTime_END: "",
          endTime_BEGIN: "",
          endTime_END: "",
          signupEndTime_BEGIN: "",
          signupEndTime_END: "",
        },
        startTimeDatePicker: [],
        endTimeDatePicker: [],
        signupEndTimeDatePicker: [],
        dict:{},
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      };
    },
    created() {
      this.fetchData();
    },
    mounted() {
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val;
      },
      handleInsert(row) {
        this.$refs["edit"].showEdit();
      },
      handleUpdate(row) {
        if (row.id) {
          this.$refs["edit"].showEdit(row);
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm("你确定要删除当前项吗", null, async () => {
            const { msg } = await doDelete({ id: row.id });
            this.$baseMessage(msg, "success");
            await this.fetchData();
          });
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join();
            this.$baseConfirm("你确定要删除选中项吗", null, async () => {
              const { msg } = await doDeleteAll({ ids });
              this.$baseMessage(msg, "success");
              await this.fetchData();
            });
          } else {
            this.$baseMessage("未选中任何行", "error");
            return false;
          }
        }
      },
      // 导出excel
      handleExportExcel(el){
        // 导出按钮防抖处理 默认限制为10秒
        vueButtonClickBan(el, 10);

        // 执行导出
        doExportExcel(this.queryForm);
      },
      // 导入excel
      handleImportExcel(){
        this.$refs["import"].show();
      },


      handleSizeChange(val) {
        this.queryForm.pageSize = val;
        this.fetchData();
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val;
        this.fetchData();
      },
      moreQuery(){
        this.moreQueryFlag = !this.moreQueryFlag;
      },
      queryData() {

        if(isNotNull(this.startTimeDatePicker) && this.startTimeDatePicker.length === 2){
          this.queryForm.startTime_BEGIN =
                  this.startTimeDatePicker.length === 0 ? "" : formateDate(this.startTimeDatePicker[0], 'yyyy-MM-dd hh:mm:ss');
          this.queryForm.startTime_END =
                  this.startTimeDatePicker.length === 0 ? "" : formateDate(this.startTimeDatePicker[1], 'yyyy-MM-dd hh:mm:ss');
        }else{
          this.queryForm.startTime_BEGIN = "";
          this.queryForm.startTime_END = "";
        }        
        if(isNotNull(this.endTimeDatePicker) && this.endTimeDatePicker.length === 2){
          this.queryForm.endTime_BEGIN =
                  this.endTimeDatePicker.length === 0 ? "" : formateDate(this.endTimeDatePicker[0], 'yyyy-MM-dd hh:mm:ss');
          this.queryForm.endTime_END =
                  this.endTimeDatePicker.length === 0 ? "" : formateDate(this.endTimeDatePicker[1], 'yyyy-MM-dd hh:mm:ss');
        }else{
          this.queryForm.endTime_BEGIN = "";
          this.queryForm.endTime_END = "";
        }
        if(isNotNull(this.signupEndTimeDatePicker) && this.signupEndTimeDatePicker.length === 2){
          this.queryForm.signupEndTime_BEGIN =
                  this.signupEndTimeDatePicker.length === 0 ? "" : formateDate(this.signupEndTimeDatePicker[0], 'yyyy-MM-dd hh:mm:ss');
          this.queryForm.signupEndTime_END =
                  this.signupEndTimeDatePicker.length === 0 ? "" : formateDate(this.signupEndTimeDatePicker[1], 'yyyy-MM-dd hh:mm:ss');
        }else{
          this.queryForm.signupEndTime_BEGIN = "";
          this.queryForm.signupEndTime_END = "";
        }

        this.queryForm.pageNo = 1;
        this.fetchData();
      },
      async fetchData() {
        this.listLoading = true;
        const { data } = await getList(this.queryForm);
        if(isNotNull(data)){
          this.list = data.rows;
          this.total = data.total;
        }
        setTimeout(() => {
            this.listLoading = false;
        }, 300);
      },
    },
  };
</script>
